package com.alibaba.serverless.service.filter;

import com.alibaba.serverless.common.filter.Filter;
import com.alibaba.serverless.common.filter.FilterInvoker;
import com.alibaba.serverless.common.constant.SmtpServiceConstants;
import com.alibaba.serverless.common.exception.BizException;
import com.alibaba.serverless.common.util.RedisUtils;
import com.alibaba.serverless.domain.ReceiveParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author mac_zyj
 *  一分钟内不能重复发送短信规则
 */

public class EmailOneMinNotRepeatFilter implements Filter<ReceiveParams> {

    private final Logger logger = LoggerFactory.getLogger(EmailOneMinNotRepeatFilter.class);

    private static final String SMS_ERROR_MESSAGE="请不要一分钟内重复发送";

    private RedisUtils redisService =new RedisUtils();

    @Override
    public void doFilter(ReceiveParams context, FilterInvoker nextFilter) {
        String emailAddress= context.getToAddress();
        String repeatCheckKey=emailAddress+ SmtpServiceConstants.REPEAT_CHECK;
        if(null!=redisService.get(repeatCheckKey)){
            logger.warn("SmsException with error code,error message");
            throw new BizException(SMS_ERROR_MESSAGE);
        }
        nextFilter.invoke(context);
    }
}
